package com.atguigu.service_order.service.impl;

import com.atguigu.commonutils.OrderNoUtil;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.vo.CourseOrderVo;
import com.atguigu.commonutils.vo.UcenterMemberOrderVo;
import com.atguigu.service_order.client.CourseClient;
import com.atguigu.service_order.client.UcenterClient;
import com.atguigu.service_order.entity.TOrder;
import com.atguigu.service_order.mapper.TOrderMapper;
import com.atguigu.service_order.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author clark
 * @since 2020-11-19
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {
    @Autowired
    private CourseClient courseClient;
    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String createOrder(String courseId, String userId) {
        //通过远程调用根据用户id获取用户信息
        UcenterMemberOrderVo memberOrderVo = ucenterClient.getMemberInfoOrderById(userId);
        //通过远程调用根据课程Id获取课程信息
        CourseOrderVo courseOrderVo = courseClient.getCourseOrderInfo(courseId);
        TOrder tOrder = new TOrder();
        tOrder.setOrderNo(OrderNoUtil.getOrderNo());
        tOrder.setCourseCover(courseOrderVo.getCover());
        tOrder.setCourseId(courseOrderVo.getId());
        tOrder.setCourseTitle(courseOrderVo.getTitle());
        tOrder.setTotalFee(courseOrderVo.getPrice());
        tOrder.setTeacherName(courseOrderVo.getTeacherName());
        tOrder.setMemberId(memberOrderVo.getId());
        tOrder.setMobile(memberOrderVo.getMobile());
        tOrder.setNickname(memberOrderVo.getNickname());
        tOrder.setStatus(0);
        tOrder.setPayType(1);
        baseMapper.insert(tOrder);

        return tOrder.getOrderNo();
    }
}
